package com.lvmama.rhino.web;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lvmama.rhino.pojo.comment.Enum.EnumForCategory;
import com.lvmama.rhino.pojo.superfreetravel.PageView;
import com.lvmama.rhino.pojo.superfreetravel.PageViewVo;
import com.lvmama.rhino.pojo.superfreetravel.SearchListVo;
import com.lvmama.rhino.pojo.superfreetravel.SuperfreeFlowstatistic;
import com.lvmama.rhino.service.SuperfreetravelService;
import com.lvmama.rhino.utils.Constants;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 超级自由行  2018/05/11
 */

@Controller
@RequestMapping("/superfreetravel")
public class SuperfreetravelController {

    private Logger log = Logger.getLogger(SuperfreetravelController.class);

    @Autowired
    private SuperfreetravelService superfreetravelService;

//    @Autowired
//    private RedisServiceImpl redisService;

    /**
     * 查询各元素点击量
     *@author shibing
     * @param
     * @return page
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public PageInfo<SuperfreeFlowstatistic> getList(SearchListVo searchListVo
                        //@RequestBody @RequestParam(name="chooseDate",defaultValue = "") String chooseDate,
                        //@RequestParam("currentPage") String currentPage, String pageSize
     ) {
        // 查询日期条件处理
        Map<String, Date> dateMap = getQueryDate(searchListVo.getChooseDate());
        Date beginDate = dateMap.get("beginDate");
        Date endDate = dateMap.get("endDate");
        searchListVo.setStartDate(beginDate);
        searchListVo.setEndDate(endDate);
        PageInfo<SuperfreeFlowstatistic> list=superfreetravelService.searchList(searchListVo);
        return list;
    }

/**
     * 查询各页面PV UV
     *@author shibing
     * @param
     * @return page
     */
    @RequestMapping(value = "getPageViewList")
    @ResponseBody
    public PageInfo<PageView> getPageVisitsList(PageViewVo pageViewVo) {
        // 查询日期条件处理
        Map<String, Date> dateMap = getQueryDate(pageViewVo.getChooseDate());
        Date beginDate = dateMap.get("beginDate");
        Date endDate = dateMap.get("endDate");
        pageViewVo.setStartDate(beginDate);
        pageViewVo.setEndDate(endDate);
        PageInfo<PageView> list=superfreetravelService.getPageViewList(pageViewVo);
        return list;
    }

    @RequestMapping(value = "page")
    public String getPage(){
        return "application/superfreetravel/superfreetravelList";
    }

    @RequestMapping(value = "pagevisits")
    public String getPageOfVisits(){
        return "application/superfreetravel/superfreePageVisits";
    }
    /**
     * @Title: getQueryDate
     * @Description: 查询条件起始时间的处理
     * @param @param choose_date
     * @param @return 设定文件
     * @return Map<String,Date> 返回类型
     * @throws
     */
    private Map<String, Date> getQueryDate(String choose_date) {
        Map<String, Date> map = new HashMap<>();
        if(choose_date==null || choose_date.equals("")){
            map.put("startDate",null);
            map.put("endDate",null);
        }
        DateTimeFormatter formater = DateTimeFormat
                .forPattern("MM/dd/yyyy HH:mm:ss");
        Date beginDate = DateTime.parse(
                choose_date.split("-")[0].trim() + " 00:00:00", formater)
                .toDate();
        Date endDate = DateTime.parse(
                choose_date.split("-")[1].trim() + " 23:59:59", formater)
                .toDate();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return map;
    }

}
